//
//  KKStatus.swift
//  SinaWeiBo
//
//  Created by King.Kong. on 16/5/26.
//  Copyright © 2016年 King.Kong. All rights reserved.
//

import UIKit

class KKStatus: NSObject {
    
    /// 微博信息内容
    var text:String?
    
    /// 微博创建时间
    var created_at: String?
    
    /// 微博ID
    var id:Int64 = 0
    
    ///微博来源
    var source: String?
    
    /// 用户信息
    var user: KKUser?
    
    var retweeted_status: KKStatus?
    
    //配图视图的模型数组
    var pic_urls: [KKPicture]?
    
    //转发的数量
    var reposts_count: Int = 0
    //评论
    var comments_count: Int = 0
    //赞
    var attitudes_count: Int = 0

    
    init(dict:[String : AnyObject]) {
        
        super.init()
        
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forKey key: String) {
        if key == "user" {
            if let dict = value as? [String : AnyObject] {
                
                user = KKUser.init(dict: dict)
            }
            // TODO:一定要return
            return

        }
        
        //给转发微博做字典转模型
        if key == "retweeted_status" {
            if let dict = value as? [String : AnyObject] {
                retweeted_status = KKStatus(dict: dict)
            }
            //需要添加return  不然字典转模型就白做了
            return
        }

        if key == "pic_urls" {
            //1.将value 转换为数组
            if let array = value as? [[String : AnyObject]] {
                //2.遍历数组
                var temparray = [KKPicture]()
                for item in array {
                    //3.字典转模型
                    let p = KKPicture(dict: item)
                    //添加到模型数组中
                    temparray.append(p)
                }
                //赋值保存
                pic_urls = temparray
            }
            return
        }

        //super在下面效率稍微高一点
        super.setValue(value, forKey: key)

    }
    // 过滤
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    
}
